//package com.hc.demo.controller;
//
//import com.hc.demo.domain.Person;
//import com.hc.demo.service.PersonService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.servlet.ModelAndView;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.UUID;
//
//@Controller
//public class PersonController {
//
//    //返回视图常量
//    private final static String REDIRECT_TO_LIST="redirect:/addressList";
//
//    //地址列表接口，为了不定义地址类，我们用Person类来模拟地址类
//    @RequestMapping("/addressList")
//    public ModelAndView addressList(Person person){
//        //因为要查的是地址表的某个用户的地址数据，所以要接收参数：用户name
//        List<Person> personList=new ArrayList<>();
//        for(int i=1;i<10;i++){
//            Person p=new Person();
//            p.setName("name"+i);
//            p.setPassword(UUID.randomUUID().toString());
//            personList.add(p);
//        }
//
//        return new ModelAndView("list","personList",personList);//页面短名字  数据key  数据值
//    }
//
//    @Autowired
//    PersonService personService;
//
//    @RequestMapping("/index")
//    public ModelAndView index(){
//        return new ModelAndView("login");
//    }
//
//    @RequestMapping("/login2")
//    public String login2(Person person){
//        System.out.println("return login method.....");
//        System.out.println("name:"+person.getName());
//        System.out.println("password:"+person.getPassword());
//
//
//        Person result=personService.login(person);
//
//        //return "redirect:/addressList";
//        return "forward:/addressList";
//
//        //return new ModelAndView("hc_index");
//    }
//
////    @RequestMapping("/login2")
////    public ModelAndView login2(Person person){
////        System.out.println("return login method.....");
////        System.out.println("name:"+person.getName());
////        System.out.println("password:"+person.getPassword());
////
////
////        Person result=personService.login(person);
////
////        return new ModelAndView("hc_index");
////    }
//
//    @RequestMapping("/login")
//    public ModelAndView login(String name,String password){
//        System.out.println("return login method.....");
//        System.out.println("name:"+name);
//        System.out.println("password:"+password);
//
//        Person person=new Person();
//        person.setName(name);
//        person.setPassword(password);
//
//        Person result=personService.login(person);
//
//        return new ModelAndView("hc_index");
//    }
//
////    @RequestMapping("/")
////    public ModelAndView index2(){
////        return new ModelAndView("login");
////    }
//
//    //修改接口
//    @RequestMapping("toUpdate")
//    public ModelAndView toUpdate(Person person){
//        System.out.println("in toUpdate method...");
//        System.out.println("toUpdate receive param name:"+person.getName());
//        //没有关联数据库，模拟
//        person.setPassword("0011101");
//        return new ModelAndView("update_person","person",person);
//    }
//
//    @RequestMapping("/update")
//    public String update(Person person){
//        System.out.println("update name:"+person.getName()  );
//        System.out.println("after update password:"+person.getPassword()  );
//        return REDIRECT_TO_LIST;
//    }
//
//    //删除接口
//    @RequestMapping("/delete")
//    public String delete(Person person){
//        System.out.println("delete name 为"+person.getName()+" 的用户");
//        return REDIRECT_TO_LIST;
//    }
//
//    //跳转到添加页面
//    @RequestMapping("/toAdd")
//    public ModelAndView toAdd(){
//
//        return new ModelAndView("add_person");
//    }
//    //处理客户端添加用户请求
//    @RequestMapping("/add")
//    public String add(Person person){
//        System.out.println("添加用户名与密码："+person.getName()+","+person.getPassword());
//        return REDIRECT_TO_LIST;
//    }
//
//}

package com.hc.demo.controller;

import com.hc.demo.domain.Person;
import com.hc.demo.service.PersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpSession;

@Controller
@RequiredArgsConstructor
public class PersonController {

    private final PersonService personService;

    // 检查是否登录的方法
    private boolean checkLogin(HttpSession session, Model model) {
        if (session.getAttribute("loginUser") == null) {
            model.addAttribute("error", "请先登录系统");
            return false;
        }
        return true;
    }

    // 首页 - 需要登录
    @GetMapping("/")
    public String index(HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        return "hc_index";
    }

    // 跳转到登录页
    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    // 登录处理
    @PostMapping("/login2")
    public String login(Person person, HttpSession session, Model model) {
        if (personService.login(person)) {
            // 登录成功，将用户信息存入Session
            session.setAttribute("loginUser", person.getName());
            return "redirect:/"; // 登录成功跳首页
        }
        model.addAttribute("error", "用户名或密码错误");
        return "login"; // 失败回显错误
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate(); // 清除Session
        return "redirect:/login";
    }

    // 人员列表 - 需要登录
    @GetMapping("/addressList")
    public String list(HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        model.addAttribute("personList", personService.getPersonList());
        return "list";
    }

    // 跳转到添加页 - 需要登录
    @GetMapping("/toAdd")
    public String toAdd(HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        model.addAttribute("person", new Person());
        return "add_person";
    }

    // 添加处理
    @PostMapping("/add")
    public String add(Person person, HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        if (personService.addPerson(person)) {
            return "redirect:/addressList";
        }
        model.addAttribute("error", "用户名已存在");
        model.addAttribute("person", person);
        return "add_person";
    }

    // 跳转到修改页 - 需要登录
    @GetMapping("/toUpdate")
    public String toUpdate(@RequestParam String name, HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        personService.getPersonByName(name).ifPresent(person ->
                model.addAttribute("person", person)
        );
        return "update_person";
    }

    // 修改处理
    @PostMapping("/update")
    public String update(Person person, HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        personService.updatePerson(person);
        return "redirect:/addressList";
    }

    // 删除处理
    @GetMapping("/delete")
    public String delete(@RequestParam String name, HttpSession session, Model model) {
        if (!checkLogin(session, model)) {
            return "login";
        }
        personService.deletePerson(name);
        return "redirect:/addressList";
    }
}
